﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ManagedDLL;

namespace ServerApp
{
    public sealed class ClientManager
    {
        private static readonly ClientManager instance = new ClientManager();
        private List<Client> ClientTeam1;
        private List<Client> ClientTeam2;
        private List<Client> ClientSpectator;
        public MainForm form { get; set; }

        private int PlayersOnline
        {
            get { return ClientTeam1.Count + ClientTeam2.Count + ClientSpectator.Count; }
        }

        private ClientManager() 
        {
            ClientTeam1 = new List<Client>();
            ClientTeam2 = new List<Client>();
            ClientSpectator = new List<Client>();
        }

        public static ClientManager Instance
        {
            get 
            {
                return instance; 
            }
        }

        public void AddClient(Client c, TEAM t)
        {
            RemoveAddClientToListBox(c.playername, 0);
            switch(t)
            {
                case TEAM.ONE:
                    ClientTeam1.Add(c);
                    break;
                case TEAM.TWO:
                    ClientTeam2.Add(c);
                    break;
                case TEAM.SPECTATORS:
                    ClientSpectator.Add(c);
                    break;
            }
        }

        public void RemoveClient(Client c)
        {
            if(ClientTeam1.FirstOrDefault(e => e.playername == c.playername) != null)
            {
                if (ClientTeam2.FirstOrDefault(e => e.playername == c.playername) != null)
                {
                    if (ClientSpectator.FirstOrDefault(e => e.playername == c.playername) != null)
                    {
                        ClientSpectator.Remove(c);
                    }
                }
                else
                {
                    ClientTeam2.Remove(c);  
                }
            }
            else
            {
                ClientTeam1.Remove(c);  
            }
        }

        public Client GetClient(Guid g)
        {
            Client c = ClientSpectator.FirstOrDefault(a => a.playerid == g);
            if (c == null)
            {
                c = ClientTeam1.FirstOrDefault(a => a.playerid == g);
                if (c == null)
                {
                    return ClientTeam2.FirstOrDefault(a => a.playerid == g);
                }
            }
            return c;
        }

        public void SendAllClientsChatMessage(string text, string clientname)
        {
            form.ConsoleTextBox.Invoke(new MainForm.UpdateTextCallback(form.UpdateText), new object[] { clientname + " say: " + text });
            
            foreach (Client c in ClientSpectator)
            {
                if (c.playername != clientname)
                {                    
                    c.SendChatMessage(clientname, text);                    
                }
            }

            foreach (Client c in ClientTeam1)
            {
                if (c.playername != clientname)
                {
                    c.SendChatMessage(clientname, text);
                }
            }

            foreach (Client c in ClientTeam2)
            {
                if (c.playername != clientname)
                {
                    c.SendChatMessage(clientname, text);
                }
            } 
        }

        public void SendAllPlayersPlayerPosition(PlayerInfo position, Guid cliendid)
        {
            foreach (Client c in ClientSpectator)
            {
                if (c.playerid != cliendid)
                {
                    c.SendPlayerPosition("hoi", position);
                }
            }

            foreach (Client c in ClientTeam1)
            {
                if (c.playerid != cliendid)
                {
                    c.SendPlayerPosition("hoi", position);
                }
            }

            foreach (Client c in ClientTeam2)
            {
                if (c.playerid != cliendid)
                {
                    c.SendPlayerPosition("hoi", position);
                }
            } 
        }

        public void RemoveAddClientToListBox(string playername, int addremove)
        {
            form.ConsoleTextBox.Invoke(new MainForm.UpdatePlayersListCallback(form.UpdatePlayersList), new object[] { playername, addremove });
        }
    }
}
